package com.linktrip.devicemanager.cmpp;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 
 * 身份验证
 */
public class MsgConnect extends MsgHead {
	private static Logger logger = LoggerFactory.getLogger(MsgConnect.class);
	private String sourceAddr;// 源地，此处为SP_Id，即SP的企业代码?
	private byte[] authenticatorSource;// 用于鉴别源地 MD5
										// hash计算得出，表示如下：AuthenticatorSource =
										// MD5（Source_Addr+9 字节0 +shared
										// secret+timestamp Shared secret
										// 由中国移动与源地实体事先商定，timestamp格式为：MMDDHHMMSS，即月日时分秒，10位?
	private byte version;// 双方协商的版本号(高位4bit表示主版本号,低位4bit表示次版本号)，对3.0的版本，4bit3，低4位为0
	private int timestamp;// 时间戳的明文,由客户端产生,格式为MMDDHHMMSS，即月日时分秒，10位数字的整型，右对齐 

	public byte[] toByteArry() {
		ByteArrayOutputStream bous = new ByteArrayOutputStream();
		DataOutputStream dous = new DataOutputStream(bous);
		try {
			dous.writeInt(this.getTotalLength());
			dous.writeInt(this.getCommandId());
			dous.writeInt(this.getSequenceId());
			MsgUtils.writeString(dous, this.sourceAddr, 6);
			dous.write(authenticatorSource);
			dous.writeByte(0x30);
			dous.writeInt(Integer.parseInt(MsgUtils.getTimestamp()));
			dous.close();
		} catch (IOException e) {
			logger.error("封装链接二进制数组失败?");
		}
		return bous.toByteArray();
	}

	public String getSourceAddr() {
		return sourceAddr;
	}

	public void setSourceAddr(String sourceAddr) {
		this.sourceAddr = sourceAddr;
	}

	public byte[] getAuthenticatorSource() {
		return authenticatorSource;
	}

	public void setAuthenticatorSource(byte[] authenticatorSource) {
		this.authenticatorSource = authenticatorSource;
	}

	public byte getVersion() {
		return version;
	}

	public void setVersion(byte version) {
		this.version = version;
	}

	public int getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(int timestamp) {
		this.timestamp = timestamp;
	}
}
